﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Server
{
	public partial class Form2 : Form
	{
		string path;
		public Form2(string _path)
		{
			path = _path;
			InitializeComponent();
		}


		private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Right)
			{
				ContextMenuStrip menu = new ContextMenuStrip();   //初始化menu


				menu.Items.Add("刷新");

				menu.Items[0].Click += new EventHandler(refresh);     //定义菜单项1上的Click事件处理函数
				if (dataGridView1.SelectedRows.Count >= 1)
				{
;
					menu.Items.Add("删除");

					menu.Items[1].Click += new EventHandler(del);     //定义菜单项1上的Click事件处理函数
				}if (dataGridView1.SelectedCells.Count == 1)
				{

				}
				menu.Show(dataGridView1, new Point(e.X, e.Y));   //在点(e.X, e.Y)处显示menu

			}
		}
		private void refresh(object sender, EventArgs e)
		{
			refresh();
		}
		void refresh()
		{

			dataTools data = new dataTools(configClass.databaseip, configClass.databaseusername, "music", configClass.databaseport, configClass.psw);
			dataGridView1.DataSource = data.getTable("V1");
		}
		private void del(object sender, EventArgs e)
		{
			DataGridViewSelectedRowCollection rowColl = dataGridView1.SelectedRows;



			if (rowColl == null)

				return;



			DataTable totalDT = (DataTable)dataGridView1.DataSource;

			if (totalDT == null)

				return;



			DataTable gridSelectDT = totalDT.Clone();



			for (int i = 0; i < rowColl.Count; i++)

			{


				DataRow dataRow = (rowColl[i].DataBoundItem as DataRowView).Row;

				gridSelectDT.ImportRow(dataRow);

			}

			string value = string.Empty;
			using (Form3 frm = new Form3(gridSelectDT,path))
			{
				if (frm.ShowDialog() == DialogResult.OK)
				{
					//value就是Form2关闭的时候传出的
					value = frm.OutValue;
				}
			}
			if (value == "删除成功")
			{
				refresh();
			}
			MessageBox.Show(value);
		}


		private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
		{
			string value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
			dataTools data = new dataTools(configClass.databaseip, configClass.databaseusername, "music", configClass.databaseport, configClass.psw);
			string mid = dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString();
			var headerText = dataGridView1.Columns[e.ColumnIndex].HeaderText.ToString();
			data.update("V1",new string[] {headerText,value,"mid",mid });
			 
		}

		private void Form2_Load(object sender, EventArgs e)
		{
			refresh();
		}
	}
	
}
